#include <stdio.h>
#include <malloc.h>
#define LIST_INIT_SIZE 5
#define LISTINCREMENT 3
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int ElemType;
typedef int Status;
typedef struct
{
    ElemType * elem;
    int length;
    int listsize;
}SqList;
Status InitList(SqList &L)
{
    L.elem = (ElemType*)malloc(sizeof(ElemType) * LIST_INIT_SIZE);
    if (!L.elem)
        return OVERFLOW;
    L.length = 0;
    L.listsize = LIST_INIT_SIZE;
    return OK;
}
Status DetroyList(SqList &L)
{
    free(L.elem);
    L.elem = NULL;
    return OK;
}
Status ClearList(SqList &L)
{
    L.length = 0;
    return OK;
}
Status ListEmpty(SqList L)
{
    if (L.length)
        return FALSE;
    return TRUE;
}
int ListLength(SqList L)
{
    return L.length;
}
Status GetElem(SqList L, int i, ElemType &e)
{
    if (i < 1 || i > L.length)
        return ERROR;
    e = L.elem[i-1];
    return OK;
}
int LocateElem(SqList L, ElemType e, bool (*compare)(ElemType, ElemType))
{
    for (int i = 0; i < L.length; ++i)
    {
        if (!compare(L.elem[i], e))
            return i+1;
    }
    return 0;
}
Status PriorElem(SqList L, ElemType cur_e, ElemType &pre_e)
{
    int i = 0;
    for (i = 0; i < L.length; ++i)
        if (L.elem[i] == cur_e)
            break;
    if (!i || i == L.length)
        return ERROR;
    pre_e = L.elem[i-1];
    return OK;
}
Status NextElem(SqList L, ElemType cur_e, ElemType &pre_e)
{
    int i = 0;
    for (i = 0; i < L.length - 1; ++i)
        if (L.elem[i] == cur_e)
            break;
    if (!i || i == L.length - 1)
        return ERROR;
    pre_e = L.elem[i+1];
    return OK;
}
Status ListInsert(SqList &L, int i, ElemType e)
{
    if (i < 1 || i > L.length + 1)
        return ERROR;
    if (L.length == L.listsize)
    {
        ElemType* newbase = (ElemType*)realloc(L.elem, sizeof(ElemType) * (L.length + LISTINCREMENT));
        if (!newbase)
            return OVERFLOW;
        L.elem = newbase;
        L.listsize += LISTINCREMENT;
    }
    for (int j = L.length - 1; j >= i-1; --j)
        L.elem[j+1] = L.elem[j];
    L.elem[i-1] = e;
    ++L.length;
    return OK;
}
Status ListDelete(SqList &L, int i, ElemType &e)
{
    if (i < 1 || i > L.length)
        return ERROR;
    e = L.elem[i-1];
    for (int j = i; j < L.length; ++j)
        L.elem[j-1] = L.elem[j];
    --L.length;
    return OK;
}
Status ListTraverse(SqList L, bool (*visit)(ElemType))
{
    printf("L.length = %d, L.listsize = %d\n", L.length, L.listsize);
    for (int i = 0; i < L.length; ++i)
        visit(L.elem[i]);
    printf("\n");
    return OK;
}
bool visit(ElemType e)
{
    printf("%3d", e);
    return TRUE;
}
int main(void)
{
    SqList L;
    // 初始化线性表
    InitList(L);  
    ListTraverse(L, visit);
    // 依次插入
    for (int i = 0; i < 10; ++i)  
        ListInsert(L, i, 2*i);
    ListTraverse(L, visit);
    // 删除元素
    ElemType e;
    ListDelete(L, 4, e);
    ListTraverse(L, visit);
    // visit(e);
    // 查看前后元素
    PriorElem(L, 10, e);
    visit(e);
    NextElem(L, 10, e);
    visit(e);
    // 销毁线性表
    if (DetroyList(L))
        printf("销毁成功！");

    return 0;
}